package com.lzw.bluetoothpractice.Adapters;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.lzw.bluetoothpractice.Activitys.SendMessageActivity;
import com.lzw.bluetoothpractice.Models.Attention;
import com.lzw.bluetoothpractice.Models.SendMessage;
import com.lzw.bluetoothpractice.Models._User;
import com.lzw.bluetoothpractice.R;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;


public class MyAttentionListAdapter extends RecyclerView.Adapter<MyAttentionListAdapter.ViewHolder>  {


    List<Attention> attentionList;
    View itemView;
    Context mcontext;


    public MyAttentionListAdapter(Context mcontext, List<Attention> attentions) {
        this.mcontext = mcontext;
        this.attentionList=attentions;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        itemView= LayoutInflater.from(mcontext).inflate(R.layout.user,parent,false);

        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Attention attention=attentionList.get(position);
        Log.e("atten",attention.toString());
        Glide.with(mcontext).load(attention.getAttUserHead()).into(holder.iv_head);
        holder.tv_Name.setText(attention.getAttUserName().toString());



        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


            }
        });

        holder.bt_status.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                BmobQuery<Attention> query=new BmobQuery<>();
                query.addWhereEqualTo("attedUserId",BmobUser.getCurrentUser(_User.class).getObjectId());
                query.addWhereEqualTo("attUserId",attention.getAttUserId());
                query.findObjects(new FindListener<Attention>() {
                    @Override
                    public void done(List<Attention> list, BmobException e) {
                        if(list.isEmpty()){
                            attention.save(new SaveListener<String>() {

                                @Override
                                public void done(String s, BmobException e) {
                                    holder.bt_status.setBackground(mcontext.getResources().getDrawable(R.drawable.attention_btn));

                                }
                            });
                        }else {

                             Attention attention2=new Attention();
                             attention2=list.get(0);
                            attention2.delete(attention2.getObjectId(),new UpdateListener() {

                                @Override
                                public void done(BmobException e) {

                                    holder.bt_status.setBackground(mcontext.getResources().getDrawable(R.drawable.attention_btn2));


                                }
                            });
                        }
                    }
                });


            }
        });
    }

    @Override
    public int getItemCount() {
        return attentionList.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        View view;

      TextView tv_Name;
      ImageView iv_head;
      Button bt_status;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            view=itemView;
            tv_Name=view.findViewById(R.id.tv_user_name);
            iv_head=view.findViewById(R.id.iv_head);
            bt_status=view.findViewById(R.id.bt_status);

        }
    }
}
